<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>返回套接字上的最后一个错误</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.socket-import-stream.html">« socket_import_stream</a></li>
      <li style="float: right;"><a href="function.socket-listen.html">socket_listen »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.sockets.html">Socket 函数</a></li>
    <li>返回套接字上的最后一个错误</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.socket-last-error" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">socket_last_error</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.1.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">socket_last_error</span> &mdash; <span class="dc-title">返回套接字上的最后一个错误</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.socket-last-error-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>socket_last_error</strong></span>(<span class="methodparam"><span class="type">?</span><span class="type"><span class="type"><a href="class.socket.html" class="type Socket">Socket</a></span><span class="type"></span></span> <code class="parameter">$socket</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type">int</span></div>

  <p class="para rdfs-comment">
   如果将 <span class="classname"><a href="class.socket.html" class="classname">Socket</a></span> 实例传递给此函数，将会返回指定套接字发生的最后一个错误。如果
   <code class="parameter">socket</code> 为 <strong><code>null</code></strong>, 则返回最后一个失败的套接字函数的错误码。此函数对以下情况特别有帮助：
   <span class="function"><a href="function.socket-create.html" class="function">socket_create()</a></span> 失败时不会返回套接字，以及
   <span class="function"><a href="function.socket-select.html" class="function">socket_select()</a></span> 失败的原因与特定套接字没有直接联系。
   错误码适合提供给 <span class="function"><a href="function.socket-strerror.html" class="function">socket_strerror()</a></span>，将返回指定错误码的字符串描述。
  </p>
  <p class="para">
   如果没有错误或者已经使用 <span class="function"><a href="function.socket-clear-error.html" class="function">socket_clear_error()</a></span> 清除了错误，则函数返回 <code class="literal">0</code>。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.socket-last-error-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">socket</code></dt>

     <dd>

      <p class="para">
       通过 <span class="function"><a href="function.socket-create.html" class="function">socket_create()</a></span> 创建的 <span class="classname"><a href="class.socket.html" class="classname">Socket</a></span> 实例。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.socket-last-error-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   此函数返回套接字错误码。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.socket-last-error-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
  <td>8.0.0</td>
  <td>
   现在 <code class="parameter">socket</code> 是 <span class="classname"><a href="class.socket.html" class="classname">Socket</a></span> 实例，
   之前是 <span class="type">resource</span>。
  </td>
 </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">socket</code> 现在可为空（nullable）。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.socket-last-error-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4499">
    <p><strong>示例 #1 <span class="function"><strong>socket_last_error()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$socket&nbsp;</span><span style="color: #007700">=&nbsp;@</span><span style="color: #0000BB">socket_create</span><span style="color: #007700">(</span><span style="color: #0000BB">AF_INET</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOCK_STREAM</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOL_TCP</span><span style="color: #007700">);<br /><br />if&nbsp;(</span><span style="color: #0000BB">$socket&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$errorcode&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_last_error</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$errormsg&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">$errorcode</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"Couldn't&nbsp;create&nbsp;socket:&nbsp;[</span><span style="color: #0000BB">$errorcode</span><span style="color: #DD0000">]&nbsp;</span><span style="color: #0000BB">$errormsg</span><span style="color: #DD0000">"</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.socket-last-error-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    <span class="function"><strong>socket_last_error()</strong></span> 不会清除错误代码，如果需要可以使用
    <span class="function"><a href="function.socket-clear-error.html" class="function">socket_clear_error()</a></span> 清除。
   </p>
  </p></blockquote>
 </div>


</div></div></div></body></html>